/****************************************************************
   infile moments from the SIPP data
*****************************************************************/

*by Xiaodong Fan, fanxiaodong@gmail.com

#delimit ;

***********************************************;
**  Moments from the sipp data;
************************************************;
local lvdatamoments lfpr_d lnw_d sdlnw_d lnw_fd_d lfpr1to0_d lfpr0to1_d;
infile col t `lvdatamoments' using ../../bpfcodes_com/data/raw_sipp_moments_age_v2.raw, clear;
keep if col == ${gIcol};
drop col;

*-999 means missing in the SIPP data;
*replace lnw_fd_d = . if t>${gvdata10};
foreach iv in `lvdatamoments' {;
   replace `iv' = . if `iv'<=0;
};

*aggregate level ${gvE2U} ${gvU2E};
sum lfpr1to0_d if t==1;
gen alfpr1to0_d = r(mean);
sum lfpr0to1_d if t==1;
gen alfpr0to1_d =  r(mean);
drop if t<10;

sort t;
compress;
save ${gdtadir}/dta_sipp_Moments.dta, replace; 

* lfpr_diff;
*infile t lfpr_diff_d using ../../bpfcodes_com/data/raw_lfpr_health.raw, clear;
infile t h3exce_hat h3good_hat h3bad_hat h3disab_hat
         h3_exce2exce_hat h3_exce2good_hat h3_exce2bad_hat h3_exce2disab_hat 
         h3_good2exce_hat h3_good2good_hat h3_good2bad_hat h3_good2disab_hat
         h3_bad2exce_hat h3_bad2good_hat h3_bad2bad_hat h3_bad2disab_hat
         lfpr_diff_E2G_d lfpr_diff_G2B_d lfpr_diff_B2D_d
      using ../../bpfcodes_com/data/raw_health_CPS_hs.raw, clear; 
keep t lfpr_diff_E2G_d lfpr_diff_G2B_d lfpr_diff_B2D_d;

keep if t>=${gvdata_diff} & t<=${gvdata10};
sort t;
merge 1:1 t using ${gdtadir}/dta_sipp_Moments.dta, nogenerate;
sort t;
save ${gdtadir}/dta_sipp_Moments.dta, replace;

* part time working --- exclusive with 6 LFPR difference;
infile col t lfpr_pt_d using ../../bpfcodes_com/data/raw_sipp_moments_age_workpt.raw, clear; 
keep if t>=18 & t<=80 & col==${gIcol};
replace lfpr_pt_d = . if lfpr_pt_d < 0;

sort t;
drop col;
merge 1:1 t using ${gdtadir}/dta_sipp_Moments.dta, nogenerate;
sort t;
save ${gdtadir}/dta_sipp_Moments.dta, replace;

* consumption data;
infile t C_d C_highedu_d using ../../bpfcodes_com/data/raw_moments_consumption.raw, clear;
if (${gIcol}==0) {;
   keep t C_d;
};
else {;
   keep t C_highedu_d;
   ren C_highedu_d C_d;
};
compress;
sort t;
merge 1:1 t using ${gdtadir}/dta_sipp_Moments.dta, nogenerate;
sort t;
save ${gdtadir}/dta_sipp_Moments.dta, replace;

* SS Social Security age;
infile col t irecss_d ssa_d using ../../bpfcodes_com/data/raw_sipp_moments_ss.raw, clear;
compress;
keep if col == ${gIcol};
drop col;
sort t;
merge 1:1 t using ${gdtadir}/dta_sipp_Moments.dta, nogenerate;
sort t;
save ${gdtadir}/dta_sipp_Moments.dta, replace;

* the wage depreciation after unemployment spell;
infile col plnw41_d using ../../bpfcodes_com/data/raw_sipp_moments_unemp.raw, clear;
compress;
keep if col == ${gIcol};
drop col;
gen t = 0;
append using ${gdtadir}/dta_sipp_Moments.dta;
sort t;
foreach iv in plnw41_d {;
   egen x = min(`iv');
   replace `iv' = x;
   drop x;
};
drop if t==0;
sort t;
save ${gdtadir}/dta_sipp_Moments.dta, replace;


* lfpr exit rate;
infile col t lfpr_exit_d using ../../bpfcodes_com/data/raw_sipp_moments_lfpr_exit.raw, clear;
compress;
keep if col == ${gIcol};
drop col;
sort t;
merge 1:1 t using ${gdtadir}/dta_sipp_Moments.dta, nogenerate;
sort t;
save ${gdtadir}/dta_sipp_Moments.dta, replace;

